package com.xukai.springboot.datalayer.statis;

import com.xukai.springboot.entity.statistics.t_statis_datasource;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * 数据源管理Mapper
 */
@Mapper
public interface DataSourceMapper {

    /**
     * 获取数据源列表
     */
    @SelectProvider(type = DataSourceMapperProvider.class, method = "GetDataSourceList")
    public List<t_statis_datasource> GetDataSourceList(@Param("state") int state, @Param("startTime") String startTime, @Param("endTime") String endTime,
                                                       @Param("dataSourceName") String dataSourceName, @Param("count") int pageSize, @Param("start") int pageStartCount);

    /**
     * 新建数据源
     */

    @Insert("INSERT Into t_statis_datasource (`sql` ,dataSourceName,`describe`,resultType,dataRefreshTime,lastUpdateTime,createTime,state)\n" +
            "VALUES (#{sql} ,#{dataSourceName},#{describe},#{resultType},#{dataRefreshTime},#{lastUpdateTime},#{createTime},#{state})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public void AddDataSource(t_statis_datasource data);

    /**
     * 修改数据源
     */
    @Update("update  t_statis_datasource set `sql`=#{sql} ,dataSourceName=#{dataSourceName},`describe`=#{describe},resultType=#{resultType},dataRefreshTime=#{dataRefreshTime}" +
            ",state=#{state} where id=#{id}")
    public void UpdateDataSource(t_statis_datasource data);

    /**
     * 删除数据源
     */
    @Delete("delete from t_statis_datasource where id=#{id}")
    public void DelDataSource(@Param("id") int id);

    /**
     * 获取数据源详细信息
     */
    @Select("select id ,`sql` ,dataSourceName,`describe`,resultType,dataRefreshTime,lastUpdateTime,createTime,state where id=#{id}")
    public t_statis_datasource GetDataSource(@Param("id") int id);

    /**
     * 修改数据源状态
     *
     * @param id
     * @param state
     */
    @Update("update t_statis_datasource set state=#{state} where id=#{id}")
    public void UpdateState(@Param("id") int id, @Param("state") int state);

    /**
     * 更新数据源最后更新时间
     *
     * @param lastUpdateTime
     * @param dataSourceID
     */
    @Select("update t_statis_datasource set lastUpdateTime=#{lastUpdateTime} where id=#{id}")
    public void UpdateLastUPdateTime(@Param("lastUpdateTime") Date lastUpdateTime, @Param("id") int dataSourceID);

    /**
     * 判断数据源名称是否存在
     *
     * @param id
     * @return
     */
    @SelectProvider(type = DataSourceMapperProvider.class, method = "IsExist")
    @ResultType(Integer.class)
    public int IsExist(@Param("id") int id, @Param("dataSourceName") String dataSourceName);


    /**
     * 获取数据总数
     *
     * @param state
     * @param startTime
     * @param endTime
     * @param dataSourceName
     * @return
     */
    @SelectProvider(type = DataSourceMapperProvider.class, method = "GetDataSourceCount")
    @ResultType(Integer.class)
    public int GetDataSourceCount(@Param("state") int state, @Param("startTime") String startTime, @Param("endTime") String endTime,
                                  @Param("dataSourceName") String dataSourceName);

    /**
     * 数据源管理代理类
     */
    class DataSourceMapperProvider {

        /**
         * 判断数据源名称是否存在
         *
         * @param id
         * @param dataSourceName
         * @return
         */
        public String IsExist(int id, String dataSourceName) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select count(*) from t_statis_datasource where dataSourceName=#{dataSourceName}");
            if (id > -1) {
                sb.append(" and id!=#{id}");
            }
            return sb.toString();
        }

        /**
         * 获取数据源列表
         *
         * @param state
         * @param startTime
         * @param endTime
         * @param dataSourceName
         * @return
         */
        public String GetDataSourceList(int state, String startTime, String endTime, String dataSourceName, int pageSize, int pageStartCount) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select id ,dataSourceName,`sql`,`describe`,resultType,dataRefreshTime,lastUpdateTime ,createTime,state from t_statis_datasource");
            sb.append(" where 1=1 ");
            if (state > 0) {
                sb.append(" and state=#{state}");
            }
            if (!dataSourceName.isEmpty()) {
                sb.append(" and dataSourceName like concat(concat('%',#{dataSourceName}),'%') ");
            }
            if (!startTime.isEmpty()) {
                sb.append(" and createTime >=#{startTime}");
            }
            if (!endTime.isEmpty()) {
                sb.append(" and createTime<= #{endtime}");
            }

            sb.append(" order by dataSourceName");
            if (pageSize > -1) {
                sb.append(" limit #{start},#{count}");
            }

            return sb.toString();
        }

        /**
         * 获取数据总数
         *
         * @param state
         * @param startTime
         * @param endTime
         * @param dataSourceName
         * @return
         */
        public String GetDataSourceCount(int state, String startTime, String endTime, String dataSourceName) {
            StringBuilder sb = new StringBuilder();
            sb.append(" select count( id) from t_statis_datasource");
            sb.append(" where 1=1 ");
            if (state > 0) {
                sb.append(" and state=#{state}");
            }
            if (!dataSourceName.isEmpty()) {
                sb.append(" and dataSourceName like concat(concat('%',#{dataSourceName}),'%') ");
            }
            if (!startTime.isEmpty()) {
                sb.append(" and createTime >=#{startTime}");
            }
            if (!endTime.isEmpty()) {
                sb.append(" and createTime<= #{endtime}");
            }
            return sb.toString();
        }
    }
}
